<%--
  Created by IntelliJ IDEA.
  User: lilocal
  Date: 2017/7/25
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>显示用户信息</title>
    <script type="text/javascript" src="../plugs/js/jquery-1.7.2.js"></script>
</head>
<body>
<form id="loadForm" action="../user/findUser" method="post">

    <table id="Result">
        <tr>
            <td>用户名：</td>
            <td><input type="text" id="username" name="username"></td>
            <td><input type="button" id="btnSearch" name="btnSearch" onclick="LoadUserInfo()" value="查 询"></td>
            <td><input type="button" id="btnSearch" name="btnSearch" onclick="delUser()" value="删 除"></td>
            <td><input type="button" id="btnSearch" name="btnSearch" onclick="updateUser()" value="修 改"></td>
        </tr>
        <tr>
            <td>id:<input type="text" id="id" name="id"></td>
            <td>用户名:<input type="text" id="uname" name="uname"></td>
            <td>密码:<input type="text" id="password" name="password"></td>
            <td>性别:<input type="text" id="sex" name="sex"></td>
        </tr>
        <tr>
            <td>ID</td>
            <td>用户名</td>
            <td>密码</td>
            <td>性别</td>
        </tr>

    </table>


</form>
<script>

    $(function () {
        LoadUserInfo();
    });

    function LoadUserInfo() {
        var username = $("#username").val();
        $.ajax({
            type: 'post',
            url: $('#loadForm').attr('action'),
            data:{"username":username},
            dataType: 'json',
            success: function (data) {
                var html = "";
                var result = data;
                if (data.result != null) {
                    for (var i = 0; i < data.result.length; i++) {
                        html += "<tr>";
                        html += "<td>" + data.result[i].id + "</td>";
                        html += "<td>" + data.result[i].username + "</td>";
                        html += "<td>" + data.result[i].password + "</td>"
                        html += "<td>" + data.result[i].sex + "</td>"
                        html += "</tr>";
                    }
                }
                $("#Result").append(html);
            },
            error: function () {
                alert("用户信息加载失败！");
            }
        });
    }

    function delUser() {
        var id = $("#username").val();
        if (confirm("确定要删除吗?")) {
            $.ajax({
                type: 'post',
                url: '../user/del',
                data: {"id": id},
                dataType: 'json',
                success: function (data) {
                    if (data.result == "success") {
                        alert("删除成功");
                    } else {
                        alert("删除失败");
                    }
                },
                error: function () {
                    alert("删除失败");
                }
            });
        }
    }

    function updateUser()
    {

        var id = $("#id").val();
        var username = $("#uname").val();
        var password = $("#password").val();
        var sex = $("#sex").val();

        $.ajax({
            type:'post',
            url:'../user/updateUser',
            data:{"id":id,"username":username,"password":password,"sex":sex},
            dataType:'json',
            success:function(data)
            {
                if (data.result == "success") {
                    alert("修改成功");
                } else {
                    alert("修改失败");
                }
            },
            error:function()
            {
                alert("修改失败");
            }
        });
    }

</script>

</body>
</html>
